package com.ddfeiyu.controller;

import com.ddfeiyu.annotation.EnableTraceLog;
import com.ddfeiyu.annotation.TraceLog;
import com.ddfeiyu.dto.UserDto;
import com.ddfeiyu.redis.util.RedisQueueUtil;
import com.ddfeiyu.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.apache.skywalking.apm.toolkit.trace.TraceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/log")
@EnableTraceLog
@Slf4j
public class LogController {

    @Autowired
    private RedisQueueUtil redisQueueUtil;
    @Autowired
    private UserService userService;

    @TraceLog(description = "第一个日志demo")
    @Trace
    @GetMapping("/log")
    public String log(){
        String traceId = TraceContext.traceId();
        log.info("trcaeId: [ {} ]", traceId);

        userService.sayHello(new UserDto("方法带没有注解"));

        userService.sayHelloWithAnnotation(new UserDto("方法带注解"));
        return traceId;
    }

    @GetMapping("/print")
    public List<String> print(@RequestParam(name = "traceId") String traceId){
        log.info("trcaeId: [ {} ]", traceId);
        List<String> traceLogList = new ArrayList<>();
        String traceLog = "";
        while ((traceLog = redisQueueUtil.dequeue(traceId)) != null){
            log.info("trcaeId: [ {} ], traceLog: [ {} ]", traceId , traceLog);
            traceLogList.add(traceLog);
        }
        return traceLogList;
    }







}
